package users

import (
	"../common"
	"../web"
	"regexp"
	"strconv"
	"strings"
)

type ModifyUserObj struct {
	common.BaseHandler
}

func (self *ModifyUserObj) Prepare() {
	usrs := *common.FetchAllRowsPtr(
		`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY ID), ID, NAME, ISNULL(DEPARTMENT, ''),
            	 CASE EXPIRED WHEN 0 THEN 'N' ELSE 'Y' END 
           FROM SFC_EMPLOYEE(NOLOCK)`,
		common.ConnectString, new(common.Employee))

	types := *common.FetchAllRowsPtr(
		`SELECT RTRIM(PRODTYPE), RTRIM(ISNULL(TYPECHN,'')), ''
           FROM SFC_PRODUCT(NOLOCK) 
          ORDER BY BARCODE`,
		common.ConnectString, new(common.ProdType))

	curr_id := 1
	iRtnData := *common.FetchOneRowPtr(
		`SELECT COUNT(ID)
           FROM SFC_EMPLOYEE(NOLOCK)`,
		common.ConnectString, new(common.RecCount))
	recnum := iRtnData.(common.RecCount).Number
	total_id := recnum / 10
	if recnum%10 > 0 {
		total_id = total_id + 1
	}

	var rights [][2]interface{}
	sRtnData := *common.FetchAllRowsPtr(
		`SELECT RIGHTSID, RIGHTSURL 
		   FROM SFC_RIGHTS(NOLOCK)`,
		common.ConnectString, new(common.RightsInfo))
	i18nDict := common.Url_Chn
	if self.Lang == "en_US" {
		i18nDict = common.Url_Eng
	}
	for _, val := range sRtnData {
		rightid := val.(common.RightsInfo).RightsId
		righturl := strings.TrimSpace(val.(common.RightsInfo).RightsUrl)
		rights = append(rights, [2]interface{}{rightid, i18nDict[righturl]})
	}

	self.TmplData["usrs"] = usrs
	self.TmplData["curr_page"] = curr_id
	self.TmplData["total_page"] = total_id
	self.TmplData["rights"] = rights
	self.TmplData["types"] = types
}

func (self *ModifyUserObj) ParameterCheck() bool {
	s_idxd := strings.TrimSpace(self.Ctx.Params["userid"])
	s_name := strings.TrimSpace(self.Ctx.Params["username"])
	s_dept := strings.TrimSpace(self.Ctx.Params["department"])
	s_expi := self.Ctx.Params["expired"]
	s_crrt := ""
	for k, _ := range self.Ctx.Params {
		if m, _ := regexp.MatchString(rightrp, k); m {
			res := right_pattern.FindStringSubmatch(k)
			s_crrt = s_crrt + strings.TrimSpace(res[1]) + ","
		}
	}
	if s_idxd == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the UserID.")
		return false
	}
	if self.Usr.Username != "admin" {
		if self.Usr.Username == s_idxd {
			self.TmplData["error"] = common.GetText(self.Lang, "Can't modify yourself.")
			return false
		}
	}

	i_expi, _ := strconv.Atoi(s_expi)
	self.Sqlparas = append(self.Sqlparas, s_idxd)
	self.Sqlparas = append(self.Sqlparas, s_name)
	self.Sqlparas = append(self.Sqlparas, s_dept)
	self.Sqlparas = append(self.Sqlparas, s_crrt)
	self.Sqlparas = append(self.Sqlparas, i_expi)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{s_idxd}

	return true
}

func (self *ModifyUserObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_ModifyUserOper ?, ?, ?, ?, ?, ?`,
		common.ConnectString, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func ModifyUserHandler(ctx *web.Context) {
	self := new(ModifyUserObj)
	self.Ctx = ctx
	self.Url = "/modifyuser"
	self.Html = "modifyuser.html"
	self.Rightlist = []string{"modifyuser"}
	self.Flashstr = "Modify User %v finished."
	self.Do(self)
}
